﻿function AzureOnlineTableSchema(tableName) {
    var self = this;

    var table = ko.observable(tableName);

    var records = ko.observableArray([]);

    var fields = ko.observableArray([]);

    var types = {};

    function findField(flds, fieldName) {
        for (var i = 0; i < flds.length; i++) {
            var fld = flds[i];
            if (fld == fieldName) {
                return fld;
            }
        }
        return null;
    }
    
    ko.computed(function () {
        var recs = records();
        types = {};
        var stat = {};
        var res = [];
        for (var i = 0; i < recs.length; i++) {
            var rec = recs[i];
            var recFields = rec.fields();
            for (var j = 0; j < recFields.length; j++) {
                var recField = recFields[j];
                var fieldName = recField.name();
                var fieldType = recField.edmType();
                if (!stat[fieldName]) {
                    stat[fieldName] = 0;
                    res.push(fieldName);
                    types[fieldName] = fieldType;
                }
                stat[fieldName]++;
            }
        }
        if (!findField(res, 'PartitionKey')) {
            res.push('PartitionKey');
            types['PartitionKey'] = '';
        }
        if (!findField(res, 'RowKey')) {
            res.push('RowKey');
            types['RowKey'] = '';
        }
        if (!findField(res, 'Timestamp')) {
            res.push('Timestamp');
            types['Timestamp'] = 'Edm.DateTime';
        }
        res.sort(function (a, b) {
            if (a == 'PartitionKey') return -1;
            if (b == 'PartitionKey') return 1;
            if (a == 'RowKey') return -1;
            if (b == 'RowKey') return 1;
            if (a == 'Timestamp') return -1;
            if (b == 'Timestamp') return 1;
            if (a == 'Name') return -1;
            if (b == 'Name') return 1;
            if (a > b) return 1;
            if (a < b) return -1;
            return 0;
        });
        fields(res);
    });

    function createRecord() {
        var rec = new AzureOnlineTableRecord();

        var flds = fields();
        var allFields = [];
        for (var i = 0; i < flds.length; i++) {
            var fld = new AzureOnlineTableRecordField();
            var name = flds[i];
            var type = getTypeOfField(name);
            fld.name(name);
            fld.value('');
            fld.edmType(type);
            allFields.push(fld);
        }
        rec.fields(allFields);

        var timestamp = new DateTimeFormatter().dateFormat(new Date(), 'yyyy-MM-ddTHH:mm:ssZ');
        rec.set('Timestamp', timestamp);

        return rec;
    }

    function getTypeOfField(fieldName) {
        return types[fieldName];
    }

    self.forRecords = function (recs) {
        records(recs);
    };

    self.table = table;
    self.fields = fields;
    self.getTypeOfField = getTypeOfField;
    self.createRecord = createRecord;
}